package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;


    @Override
    public AgreementDetailVo queryAgreementVoById(Long id) {
        //查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if(leaseAgreement==null){
            return null;
        }
        //查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.getByApartmentId(leaseAgreement.getApartmentId());
        //查询公寓图片
        List<GraphInfo> queryGraphByApartId = graphInfoMapper.queryGraphByApartId(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        //查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectByRoomId(leaseAgreement.getRoomId());
        //查询房间图片
        List<GraphVo> selectByRoomId = graphInfoMapper.selectByRoomId(ItemType.ROOM, leaseAgreement.getRoomId());
        //查询支付方式
        PaymentType paymentType= paymentTypeMapper.selectPayTypeById(leaseAgreement.getPaymentTypeId());
        //查询租期信息
        LeaseTerm leaseTerm= leaseAgreementMapper.selectLeaseTermById(leaseAgreement.getLeaseTermId());
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        //设置公寓名称
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        //设置公寓图片
        agreementDetailVo.setApartmentGraphVoList(queryGraphByApartId);
        //设置房间号
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        //设置房间图片
        agreementDetailVo.setRoomGraphVoList(selectByRoomId);
        //设置支付方式
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        //设置租期月数
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        //设置租期单位
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }

    @Override
    public List<AgreementItemVo> queryAgreementItemVoList(String phone) {
        return leaseAgreementMapper.selectLeaseAgreementByPhone(phone);
    }
}




